import { distance } from '../../core/anim/PosDefine';
import game from '../../core/game';
import { UIBase } from '../../core/gui/UIBase';
import { FButton, Fview, Gtween } from '../../core/gui/UIDefine';
import { Logger } from '../../core/log/logger';
import PromiseReveal from '../../core/utils/PromiseReveal';
import Utils from '../../core/utils/Utils';
import { Badge } from '../../module/common/BadgeType';
import UI_badge from '../../moduleui/main/UI_badge';

export default class BadgeUI extends UIBase<UI_badge> {
    private btnArr: FButton[] = [];
    private sp: Laya.Sprite;

    protected createView(): UI_badge {
        return UI_badge.createInstance();
    }

    protected initialize(): void {

        let view = this.view, num = 0;

        for (let v of view._children) {
            v.group === view.m_group && v.onClick(this, this.onBtnClick);
            if (v.group === view.m_group) {
                v.data = num++;
                let btn = v.asButton;
                btn.title = v.data + '';
                this.btnArr.push(btn);
                v.onClick(this, this.onBtnClick, [v, v.data]);
            }
        }


        view.m_btn_close.onClick(this, this.close);
        let sp = (this.sp = new Laya.Sprite());
        this.view.displayListContainer.addChildAt(sp, 1);

        this.badgeRelation();
    }

    public *[Symbol.iterator]() {
        yield* [1, 3, 4]
    }

    protected onOpen(data?: any): void {

        let view = this.view, num = 0;

        this.btnArr.forEach((v, i) => {
            this.addBadge(v, Badge[`TEST${i}`]);
        })

        this.drawLine();
    }

    protected onClose(reaskon?: any): void {
        this.sp.graphics.clear();
    }

    private *fun() {
        yield* [1, 1, 1, 1];
    }

    private badgeRelation() {
        let badge = this.badge;

        for (let i = 0; i < this.btnArr.length; i++) {
            let k = Badge[`TEST${i}`], v = this.btnArr[i];
            badge.regist(k, {
                caller: this,
                method: (ages: any[]) => {
                    return Utils.parseNum(v["dataScore"]);
                },
            });
        }

        badge.registGropu(Badge.TEST0, Badge.GROUP.TEST0);
        badge.registGropu(Badge.TEST1, Badge.GROUP.TEST1);
        badge.registGropu(Badge.TEST2, Badge.GROUP.TEST2);
        badge.registGropu(Badge.TEST3, Badge.GROUP.TEST3);
        badge.registGropu(Badge.TEST4, Badge.GROUP.TEST4);
        badge.registGropu(Badge.TEST5, Badge.GROUP.TEST5);
    }

    private async drawLine() {
        await this.ligature(0, 1);
        await this.ligature(0, 2);

        await this.ligature(1, 3);
        await this.ligature(1, 4);
        await this.ligature(2, 4);
        await this.ligature(2, 5);

        await this.ligature(3, 6);
        await this.ligature(3, 7);
        await this.ligature(4, 7);
        await this.ligature(4, 8);
        await this.ligature(5, 8);
        await this.ligature(5, 9);
    }

    private onBtnClick(e: FButton, index: number) {
        let num = Number(e["dataScore"]);
        num = isNaN(num) ? 0 : num;
        e["dataScore"] = num > 0 ? 0 : 1;

        e.text = `${index}-${Utils.parseNum(e["dataScore"])}`;
        this.badge.event(Badge[`TEST${index}`]);

        console.log(e);
    }

    private setRelationBadge(type: Badge, view: Fview) {

    }

    private ligature(start: number, end: number) {
        let item1 = this.btnArr[start];
        let item2 = this.btnArr[end];

        let pos1 = [item1.x + (item1.width >> 1), item1.y + (item1.height >> 1)];
        let pos2 = [item2.x + (item2.width >> 1), item2.y + (item2.height >> 1)];

        let speedNum = 5;
        let fx = (pos2[1] - pos1[1]) / (pos2[0] - pos1[0]);
        let x1 = pos1[0],
            speed = pos1[0] > pos2[0] ? -speedNum : speedNum;
        let pos = pos1;
        let graph = this.sp.graphics;

        let pms = PromiseReveal.create();

        Laya.timer.frameLoop(1, this, () => {
            x1 += speed;
            let y1 = (x1 - pos1[0]) * fx + pos1[1];
            if (Utils.distances(x1, y1, pos2[0], pos2[1]) <= speedNum) {
                (x1 = pos2[0]), (y1 = pos2[1]);
            }

            graph.drawLine(pos[0], pos[1], x1, y1, '#FF0000', 10);
            pos = [x1, y1];

            if (x1 === pos2[0]) {
                Laya.timer.clearAll(this);
                pms.resolve();
            }
        });

        return pms;
    }
}
